﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FullRefreshService.Logging
{
    public class Logger : ILogger
    {
        private static readonly object lockObj = new object();
        private List<ILogger> _loggers = new List<ILogger>();

        private static Logger _instance;// = new Lazy<Logger>();
        public static Logger Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Logger();
                return _instance;
            }
        }

        #region ILogger Members

        public void WriteLine(string message)
        {
            lock (lockObj)
            {
                foreach (var logger in _loggers)
                {
                    logger.WriteLine(message);
                }
            }
        }

        #endregion

        public void RegisterLogger(ILogger logger)
        {
            if (!_loggers.Contains(logger))
                _loggers.Add(logger);
        }

        public void UnregisterLogger<T>() where T : ILogger
        {
            lock (lockObj)
            {
                int removeIndex = -1;
                for (int i =0; i < _loggers.Count; i++)
                {
                    if (_loggers[i] is T)
                    {
                        removeIndex = i;
                        break;
                    }
                }

                if (removeIndex >= 0)
                {
                    _loggers.RemoveAt(removeIndex);
                }
            }
        }

        //public ILogger GetLogger<T>() where T : ILogger
        //{
        //    ILogger retVal = null;

        //}
    }
}
